package ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.fy.fyzf.R;
import com.fy.fyzf.base.BaseFragment;
import com.fy.fyzf.bean.BaseData;
import com.fy.fyzf.bean.HousesListBean;
import com.fy.fyzf.bean.RecordBean;
import com.fy.fyzf.bean.UserInfoBean;
import com.fy.fyzf.glideUtils.GlideUtils;
import com.fy.fyzf.presenter.SelfPresenter;
import com.fy.fyzf.utils.MathUtils;
import com.fy.fyzf.utils.SPUtils;
import com.fy.fyzf.view.SelfInfoCallBack;
import com.fy.fyzf.weight.DragBubbleView;
import com.google.gson.Gson;
import com.paradoxie.autoscrolltextview.VerticalTextview;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import ui.activity.AboutUsActivity;
import ui.activity.IntegralStoreActivity;
import ui.activity.MessageActivity;
import ui.activity.ModificationPwdActivity;
import ui.activity.MyAllHousingActivity;
import ui.activity.MySignContractActivity;
import ui.activity.SelfInfoActivity;
import ui.activity.SettingActivity;
import ui.activity.WebviewActivity;
import ui.activity.WorkCheckActivity;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link SelfInfoFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class SelfInfoFragment extends BaseFragment<SelfPresenter> implements SelfInfoCallBack {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    @BindView(R.id.iv_logo)
    ImageView ivLogo;
    @BindView(R.id.tv_intergral)
    TextView tvIntergral;
    @BindView(R.id.msg_enter)
    RelativeLayout msgEnter;
    @BindView(R.id.tv_check_work)
    TextView tvCheckWork;
    @BindView(R.id.tv_intergral_store)
    TextView tvIntergralStore;
    @BindView(R.id.tv_self_info)
    TextView tvSelfInfo;
    @BindView(R.id.tv_change_pwd)
    TextView tvChangePwd;
    @BindView(R.id.tv_about_us)
    TextView tvAboutUs;
    @BindView(R.id.tv_user_agreement)
    TextView tvUserAgreement;
    @BindView(R.id.tv_setting)
    RelativeLayout tvSetting;
    @BindView(R.id.tv_account)
    TextView tvAccount;
    @BindView(R.id.tv_real_name)
    TextView tvRealName;
    @BindView(R.id.tv_role)
    TextView tvRole;
    @BindView(R.id.tv_company)
    TextView tvCompany;
    @BindView(R.id.tv_my_sign)
    TextView tMySign;
    @BindView(R.id.dragBubble)
    DragBubbleView dragBubbleView;
    @BindView(R.id.textBanner)
    VerticalTextview textBanner;

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public SelfInfoFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment SelfInfoFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static SelfInfoFragment newInstance(String param1, String param2) {
        SelfInfoFragment fragment = new SelfInfoFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
        registerEventBus(this);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unregisterEventBus(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(String s) {
        if (s.equals("exchangeSuccess")) {
            loadData();
        }
    }
    @Override
    public void initView(View rootView) {
        super.initView(rootView);
        dragBubbleView.setOnBubbleStateListener(new DragBubbleView.OnBubbleStateListener() {
            @Override
            public void onDrag() {

            }

            @Override
            public void onMove() {

            }

            @Override
            public void onRestore() {

            }

            @Override
            public void onDismiss() {
                mPresenter.setAllRead();
            }
        });
    }

    @Override
    protected SelfPresenter createPresenter() {
        return new SelfPresenter(this);
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.fragment_self_info;
    }

    @Override
    protected void loadData() {
        mPresenter.getChangeMsg();
        mPresenter.getSelfInfo();
    }

    @OnClick({R.id.tv_my_housing,R.id.ll_self,R.id.tv_my_sign,R.id.msg_enter, R.id.tv_check_work, R.id.tv_intergral_store, R.id.tv_self_info, R.id.tv_change_pwd, R.id.tv_about_us, R.id.tv_user_agreement, R.id.tv_setting})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_my_housing:
                startActivity(new Intent(getActivity(), MyAllHousingActivity.class));
                break;
            case R.id.ll_self:
                startActivity(new Intent(getActivity(), SelfInfoActivity.class));
                break;
            case R.id.tv_my_sign:
                startActivity(new Intent(getActivity(), MySignContractActivity.class));
                break;
            case R.id.msg_enter:
                startActivity(new Intent(getActivity(), MessageActivity.class));
                break;
            case R.id.tv_check_work:
                startActivity(new Intent(getActivity(), WorkCheckActivity.class));
                break;
            case R.id.tv_intergral_store:
                Intent intent = new Intent(getActivity(), IntegralStoreActivity.class);
                intent.putExtra("integral", integral);
                startActivity(intent);
                break;
            case R.id.tv_self_info:
                startActivity(new Intent(getActivity(), SelfInfoActivity.class));
                break;
            case R.id.tv_change_pwd:
                startActivity(new Intent(getActivity(), ModificationPwdActivity.class));
                break;
            case R.id.tv_about_us:
                startActivity(new Intent(getActivity(), AboutUsActivity.class));
                break;
            case R.id.tv_user_agreement:
                intent = new Intent(getActivity(), WebviewActivity.class);
                intent.putExtra("url", "https://www.fuyinzf.com/agreement/2.html");
                startActivity(intent);
                break;
            case R.id.tv_setting:
                startActivity(new Intent(getActivity(), SettingActivity.class));
                break;
        }
    }

    private int integral;

    @Override
    public void getSelfInfoSuccess(UserInfoBean userInfoBean) {
        if (userInfoBean.getUnReadMsg() == 0) {
            dragBubbleView.setVisibility(View.GONE);
        } else {
            dragBubbleView.setVisibility(View.VISIBLE);
        }
        dragBubbleView.setText(String.valueOf(userInfoBean.getUnReadMsg()));
        GlideUtils.loadCircleImage(userInfoBean.getUserPortrait(), ivLogo);
        integral = userInfoBean.getIntegral();
        SPUtils.getInstance(getActivity()).putString("logo", userInfoBean.getUserPortrait());
        tvAccount.setText("我的账号："+userInfoBean.getUserAccount());
        tvRealName.setText("真实姓名："+userInfoBean.getUserName());
        tvRole.setText("用户角色："+userInfoBean.getUserType());
        String s = userInfoBean.getUserBranch() == null ? "富银总部" : userInfoBean.getUserBranch();
        tvCompany.setText("所属公司："+s);
        tvIntergral.setText("我的积分："+userInfoBean.getIntegral()+"");
    }

    @Override
    public void upLoadImgSuccess(BaseData baseData) {

    }

    @Override
    public void updataSlefInfoSuccess(BaseData baseData) {

    }

    @Override
    public void getMsgCountSuccess(BaseData baseData) {
        Log.e("getMsgCountSuccess", new Gson().toJson(baseData));

    }

    @Override
    public void changePwdSuccess(BaseData baseData) {

    }
    ArrayList<String> titleList = new ArrayList<>();
    @Override
    public void getChangeMsgSuccess(List<RecordBean> list) {
        if ( null == list || list.size() == 0) {
            return;
        }
        for (int i = 0; i < list.size(); i++) {
            titleList.add("【"+list.get(i).getCompanyName()+"】"+ list.get(i).getUserName()+"兑换了"+list.get(i).getMallTitle());
        }
        textBanner.setTextList(titleList);//加入显示内容,集合类型
        textBanner.setText(12, 5, getResources().getColor(R.color.color_theme));//设置属性,具体跟踪源码
        textBanner.setTextStillTime(3000);
        textBanner.setAnimTime(300);
        textBanner.startAutoScroll();
        textBanner.setOnItemClickListener(new VerticalTextview.OnItemClickListener() {
            @Override
            public void onItemClick(int i) {
                startActivity(new Intent(getActivity(), IntegralStoreActivity.class));
            }
        });

    }

    @Override
    public void getMyHousingSuccess(HousesListBean housesListBean) {

    }
}